package com.ui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import com.entidades.MailConfig;
import com.util.Constantes;
import com.util.Utiles;

public class ConfigTabbed extends JFrame {

//	private JPanel contentPane;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextArea txtModels;
	private JTextArea txtEngineer;
	private JTextField txtUsr;
	private JTextField txtPasswd;
	private JTextField txtTo;
	private JTextField txtFrom;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ConfigTabbed frame = new ConfigTabbed();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ConfigTabbed() {
		/*
		 * load mail config obj*/
		
		MailConfig mc = unSerializeObj(Constantes.ARCHIVO_CONF_MAIL);
		
		setTitle("..:::Config:::..");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 450);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addComponent(tabbedPane, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addComponent(tabbedPane, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
		);
		
		JPanel panelUI = new JPanel();
		tabbedPane.addTab("UI", null, panelUI, null);
		
		JLabel lblModels = new JLabel("Models");
		//JLabel lblTo = new JLabel("To:");
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel lblEngineer = new JLabel("Engineer");
		
		JScrollPane scrollPane_1 = new JScrollPane();
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//ACTIONS SAVE BUTTON
				String s = txtModels.getText();
				String names = txtEngineer.getText();
				new Utiles().formatearParaGuardar(s,Constantes.ARCHIVO_INI_CONFIGURACION);
				new Utiles().formatearParaGuardar(names, Constantes.ARCHIVO_INI_NOMBRES,"");
				
				
			}
		});
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeWnd();
				
			}
		});
		
		GroupLayout gl_panelUI = new GroupLayout(panelUI);
		gl_panelUI.setHorizontalGroup(
			gl_panelUI.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelUI.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panelUI.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_panelUI.createSequentialGroup()
							.addComponent(lblModels)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 111, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panelUI.createSequentialGroup()
							.addComponent(lblEngineer)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(scrollPane_1)))
					.addContainerGap(255, Short.MAX_VALUE))
				.addGroup(Alignment.TRAILING, gl_panelUI.createSequentialGroup()
					.addContainerGap(230, Short.MAX_VALUE)
					.addComponent(btnSave)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnCancel)
					.addGap(5))
		);
		
		gl_panelUI.setVerticalGroup(
				gl_panelUI.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_panelUI.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_panelUI.createParallelGroup(Alignment.LEADING)
							.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 155, GroupLayout.PREFERRED_SIZE)
							.addComponent(lblModels))
						.addGroup(gl_panelUI.createParallelGroup(Alignment.TRAILING)
							.addGroup(Alignment.LEADING, gl_panelUI.createSequentialGroup()
								.addGap(25)
								.addComponent(lblEngineer))
							.addGroup(gl_panelUI.createSequentialGroup()
								.addGap(37)
								.addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
								.addGroup(gl_panelUI.createParallelGroup(Alignment.BASELINE)
									.addComponent(btnSave)
									.addComponent(btnCancel))))
						.addContainerGap())
						
			);
		
		
		List<String> snombres =  new Utiles().leerTodo(Constantes.ARCHIVO_INI_NOMBRES);
		String snombres2 = new Utiles().convertirACadenaSencilla(snombres);
		txtEngineer = new JTextArea(snombres2);
		scrollPane_1.setViewportView(txtEngineer);
		
		
		List<String> s =  new Utiles().leerTodo(Constantes.ARCHIVO_INI_CONFIGURACION);
		String s2 = new Utiles().convertirACadenaSencilla(s);
		txtModels = new JTextArea(s2);
		scrollPane.setViewportView(txtModels);
		panelUI.setLayout(gl_panelUI);
		
		JPanel Mail = new JPanel();
		tabbedPane.addTab("Mail", null, Mail, null);
		
		JLabel lblUser = new JLabel("User:");
		
		JLabel lblPassword = new JLabel("Password:");
		
		JButton btnSaveMail = new JButton("Save");
		btnSaveMail.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
								
					MailConfig mc = new MailConfig();
					
					mc.setUser(txtUsr.getText());
					mc.setStrpwd(txtPasswd.getText());
					mc.setTo(txtTo.getText());
					mc.setFrom(txtFrom.getText());
					
					if(serializeObj(mc, Constantes.ARCHIVO_CONF_MAIL))
					{
						JOptionPane.showMessageDialog(null, "Ok!");
					}
				
				
			}
		});
		
		JButton btnCancelMail = new JButton("Cancel");
		btnCancelMail.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeWnd();
			}
		});
		
		/*txtUsr = new JTextField();
		txtPasswd = new JPasswordField();
		if(mc==null)
		{
			txtUsr = new JTextField();
		}
		else{
		txtUsr = new JTextField(mc.getUser());
		}
		txtUsr.setColumns(10);
		
		if(mc==null)
		{
			txtPasswd = new JPasswordField();
		}
		else{
		txtPasswd = new JPasswordField(mc.getStrpwd());
		}
		txtPasswd.setColumns(10);
		
		
		JLabel lblTo_1 = new JLabel("To");
		
		txtTo = new JTextField();
		txtTo.setColumns(10);
		
		JLabel lblFrom = new JLabel("From");
		
		txtFrom = new JTextField();
		txtFrom.setColumns(10);*/
		
		if(mc==null){
			txtUsr = new JTextField();
			txtPasswd = new JPasswordField();
			txtTo = new JTextField();
			txtFrom = new JTextField();
			}
			else{
			txtUsr = new JTextField(mc.getUser());
			txtPasswd = new JPasswordField(mc.getStrpwd());
			txtTo = new JTextField(mc.getTo());
			txtFrom = new JTextField(mc.getFrom());

			}

			txtUsr.setColumns(10);
			txtPasswd.setColumns(10);
			txtTo.setColumns(10);
			txtFrom.setColumns(10);


			JLabel lblTo_1 = new JLabel("To");
			JLabel lblFrom = new JLabel("From");

		
		
		GroupLayout gl_Mail = new GroupLayout(Mail);
		gl_Mail.setHorizontalGroup(
			gl_Mail.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_Mail.createSequentialGroup()
					.addContainerGap(274, Short.MAX_VALUE)
					.addComponent(btnSaveMail)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnCancelMail)
					.addGap(17))
				.addGroup(Alignment.LEADING, gl_Mail.createSequentialGroup()
					.addGroup(gl_Mail.createParallelGroup(Alignment.TRAILING, false)
						.addGroup(Alignment.LEADING, gl_Mail.createSequentialGroup()
							.addContainerGap()
							.addComponent(lblTo_1)
							.addGap(18)
							.addComponent(txtTo))
						.addGroup(Alignment.LEADING, gl_Mail.createSequentialGroup()
							.addContainerGap()
							.addComponent(lblFrom)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(txtFrom))
						.addGroup(Alignment.LEADING, gl_Mail.createSequentialGroup()
							.addGap(10)
							.addGroup(gl_Mail.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_Mail.createSequentialGroup()
									.addComponent(lblUser)
									.addGap(4)
									.addComponent(txtUsr, GroupLayout.PREFERRED_SIZE, 159, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_Mail.createSequentialGroup()
									.addComponent(lblPassword)
									.addGap(4)
									.addComponent(txtPasswd, GroupLayout.PREFERRED_SIZE, 135, GroupLayout.PREFERRED_SIZE)))))
					.addContainerGap(220, Short.MAX_VALUE))
		);
		gl_Mail.setVerticalGroup(
			gl_Mail.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_Mail.createSequentialGroup()
					.addGap(11)
					.addGroup(gl_Mail.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_Mail.createSequentialGroup()
							.addGap(3)
							.addComponent(lblUser))
						.addComponent(txtUsr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(27)
					.addGroup(gl_Mail.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_Mail.createSequentialGroup()
							.addGap(3)
							.addComponent(lblPassword))
						.addComponent(txtPasswd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(57)
					.addGroup(gl_Mail.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblTo_1)
						.addComponent(txtTo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_Mail.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblFrom)
						.addComponent(txtFrom, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 153, Short.MAX_VALUE)
					.addGroup(gl_Mail.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnSaveMail)
						.addComponent(btnCancelMail))
					.addContainerGap())
		);
		Mail.setLayout(gl_Mail);
		contentPane.setLayout(gl_contentPane);
	}
		
	
	public void closeWnd()
	{
		dispose();
	}
	
	public boolean serializeObj(Object toSave, String ruta) {
		
		try {
			FileOutputStream fo = new FileOutputStream(ruta);
			ObjectOutputStream oo = new ObjectOutputStream(fo);
			oo.writeObject(toSave);
			oo.close();
			fo.close();
			return true;
						
		} catch (Exception e) {
			System.err.println("Error: " + e.getLocalizedMessage());
			return false;
		}
		
	}
	
	public MailConfig unSerializeObj(String ruta) {
		
		MailConfig mailCfg = null;
	
			try {
			
				FileInputStream fileIn = new FileInputStream(ruta);
		        ObjectInputStream in = new ObjectInputStream(fileIn);
		        mailCfg = (MailConfig) in.readObject();
		        in.close();
		        fileIn.close();
		        
			
			} catch (Exception e) {
				System.err.println("Error: " + e.getLocalizedMessage());	
			
			}
		
			return mailCfg;
		}
		
		
	}

